package com.situ.day26;

import org.junit.Test;

import java.util.*;

public class CollectionDemo {

    @Test
    public void test1() {
        Collection<String> collection = new ArrayList<>();
        collection.add("张三");
        collection.add("李四");
        collection.add("王五");
        collection.add("张三");
        System.out.println(collection);
        collection.remove("张三");
        System.out.println(collection);
        Object[] array = collection.toArray();
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }

    @Test
    public void test2() {
        Collection<String> collection = new ArrayList<>();
        collection.add("张三");
        collection.add("李四");
        collection.add("王五");
        collection.add("张三");
        for (String str : collection) {
            System.out.println(str);
        }
        System.out.println("------------");
        Iterator<String> iterator = collection.iterator();
        while (iterator.hasNext()) {//判断有没有下一个，有下一个就返回true
            String data = iterator.next();
            System.out.println(data);
        }

    }

    @Test
    public void test3() {
        List<String> list = new ArrayList<String>();
        list.add("abc1");
        list.add("abc2");
        list.add("abc3");
        list.add("abc4");

        list.add(2, "Java");//List接口定义的方法
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            System.out.println(next);
        }
        System.out.println("----------------");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        System.out.println("-----------------");
        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            //java.util.ConcurrentModificationException
            String s = it.next();
            if (s.equals("abc3")) {
                list.add("ABC3");
            }
            System.out.println(s);
        }

    }

    @Test
    public void ttt() {
        List<String> list = new ArrayList<String>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        for (String item : list) {
            if (item.equals("3")) {
                System.out.println(item);
                list.remove(item);
            }
        }
        System.out.println(list.size());
    }

    @Test
    public void ttt1() {
        List<String> list = new ArrayList<String>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String s = iterator.next();
            if (s.equals("3")) {
                iterator.remove();
            }
            System.out.println(s);
        }
    }

    @Test
    public void test551() {
        // 创建HashSet对象
        Set<String> set = new HashSet<String>();
        // 给集合中添加自定义对象
        set.add("zhangsan");
        set.add("lisi");
        set.add("wangwu");
        set.add("zhangsan");
        set.add(new String("lisi"));
        for (String string : set) {
            System.out.println(string);
        }
    }

    @Test
    public void test33() {
        Student student1 = new Student(1, "zhansgan1", 23);
        Student student2 = new Student(2, "zhansgan2", 23);
        Set<Student> set = new HashSet<>();
        set.add(student1);
        set.add(student2);
        for (Student student : set) {
            System.out.println(student);
        }
    }

    @Test
    public void test34() {
        Student student1 = new Student(1, "zhansgan", 23);
        Student student2 = new Student(1, "zhansgan", 24);
        Set<Student> set = new HashSet<>();
        set.add(student1);
        set.add(student2);
        for (Student student : set) {
            System.out.println(student);
        }
    }
}
